<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="车场管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="600px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="选择站点" prop="stationId">
                <el-select v-model="formData.stationId" :value-key="formData.stationId" filterable placeholder="请选择">
                    <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="车场名称" prop="carParkName">
                <el-input v-model="formData.carParkName"/>
            </el-form-item>
            <el-form-item label="车场简称" prop="carParkNameSimple">
                <el-input v-model="formData.carParkNameSimple"/>
            </el-form-item>
            <el-form-item label="详细地址" prop="address">
                <el-input v-model="formData.address">
                    <template #append>
                        <el-button slot="reference" @click="showMap()">点击选择</el-button>
                    </template>
                </el-input>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark"/>
            </el-form-item>

            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="formData.sort"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存车场吗？'" @onConfirm="submitForm(this)">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>

        <GMapSelect ref="mapRef" @bindMapInfo="bindMapInfo"/>
    </el-dialog>
</template>

<script>
import {CarParkApi} from '@/api/intercity/CarParkApi'
import {StationApi} from '@/api/intercity/StationApi'
import GMapSelect from '@/components/Map/GMapSelect.vue'
import {Common} from '@/utils/common'

export default {
    name: 'CarParkUpdate',
    components: {
        GMapSelect
    },
    data() {
        return {
            formData: {
                id: 0,
                stationId: '',
                carParkName: '',
                carParkNameSimple: '',
                districtCode: '',
                address: '',
                lng: '',
                lat: '',
                remark: '',
                sort: 0
            },
            rules: {
                stationId: [
                    {required: true, message: '请选择站点', trigger: 'change'}
                ],
                carParkName: [
                    {required: true, message: '请输入站点名称', trigger: 'blur'},
                    {min: 1, max: 10, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                ],
                carParkNameSimple: [
                    {required: true, message: '请输入站点简称', trigger: 'blur'},
                    {min: 1, max: 10, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                ],
                address: [
                    {required: true, message: '请输入详细地址', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            options: []

        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.getStationList()
            if (data.id > 0) {
                data.stationId = data.stationId.toString()
                this.$nextTick(() => {
                    this.formData = Common.assign(this.formData, data)
                })
            } else {
                this.formData.id = 0
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }
            }
        },

        // 获取城市列表
        getStationList() {
            StationApi.getList().then((data) => {
                data.forEach(function (item, index, arr) {
                    arr[index]['value'] = data[index].id.toString()
                    arr[index]['label'] = data[index].stationName
                })

                this.options = data
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        CarParkApi.update(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        CarParkApi.insert(that.formData).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        },

        showMap() {
            this.$refs['mapRef'].showMap(this.formData)
        },

        bindMapInfo(data) {
            this.formData.address = data.address
            this.formData.districtCode = data.districtCode
            this.formData.lng = data.lng
            this.formData.lat = data.lat
        }
    }
}
</script>
